/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.Date;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import pojo.Chitietdaugia;
import pojo.Sanpham;
import pojo.Taikhoan;
import util.HibernateUtil;

/**
 *
 * @author WMThiet
 */
public class ChiTietDauGiaDAO {

    public static int pageCount;
    public static int listCount; // Số lượng của danh sách

    public static int tinhTrangTheoDSSP(int soLuong, int soLuongSanPhamTrenTrang) {
        int soTrang = 0;

        if (soLuongSanPhamTrenTrang == -1) {
            soTrang = 1;
        } else {
            soTrang = soLuong / soLuongSanPhamTrenTrang;
            if (soLuong % soLuongSanPhamTrenTrang != 0) {
                soTrang++;
            }
        }
        return soTrang;
    }

    public static boolean ThemChiTietDauGia(Sanpham sp, Taikhoan taiKhoan, long giaDau) {
        boolean result = false;
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();

            //Kiểm tra giá sản phẩm:
            String hql = "from Chitietdaugia ct where ct.sanpham.maSanPham =:masp and ct.giaDau >= :giadau";
            Query query = session.createQuery(hql);
            query.setInteger("masp", sp.getMaSanPham());
            query.setLong("giadau", giaDau * 1000);
            if (query.uniqueResult() != null) {
                return false;
            }

            Chitietdaugia ctDauGia = new Chitietdaugia();
            ctDauGia.setGiaDau(giaDau * 1000);
            ctDauGia.setSanpham(sp);
            ctDauGia.setTaikhoan(taiKhoan);
            ctDauGia.setThoiGiandaugia(new Date());

            session.save(ctDauGia);

            //Cap nhat san pham:
            sp.setGiaHienTai(giaDau * 1000);
            sp.setSoLuongdaugia(sp.getSoLuongdaugia() + 1);
            session.update(sp);

            transaction.commit();
            result = true;

        } catch (HibernateException ex) {
            transaction.rollback();
            result = false;
        } finally {
            session.close();
        }
        return result;
    }

    public static List<Sanpham> LaySanPhamQuanTam(Taikhoan tk) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        List<Sanpham> listSP = null;
        try {
            session.beginTransaction();
            String hql = "select distinct ct.sanpham from Chitietdaugia ct where ct.taikhoan=:tk ";
            hql += "order by ct.thoiGiandaugia";
            Query query = session.createQuery(hql);
            query.setParameter("tk", tk);
            query.setMaxResults(6);

            listSP = query.list();

        } catch (Exception ex) {
            listSP = null;
        } finally {
            session.close();
        }
        return listSP;
    }

    public static List<Sanpham> LaySanPhamDangThamGiaDauGia(Taikhoan tk, int page) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        List<Sanpham> listSP = null;
        try {
            session.beginTransaction();
            String hql = "select distinct ct.sanpham from Chitietdaugia ct where ct.taikhoan=:tk and ct.sanpham.ngayHetHan >:homnay ";
            hql += "order by ct.thoiGiandaugia";
            Query query = session.createQuery(hql);
            query.setParameter("tk", tk);
            query.setDate("homnay", new Date());

            int numberProducts = 4;
            // Tính số trang :
            listSP = query.list();
            pageCount = tinhTrangTheoDSSP(listSP.size(), numberProducts);
            listCount = listSP.size();

            int n = (page - 1) * numberProducts;

            if (numberProducts != -1) {
                query.setFirstResult(n);
                query.setMaxResults(numberProducts);
            }
            listSP = query.list();

        } catch (Exception ex) {
            listSP = null;
        } finally {
            session.close();
        }
        return listSP;
    }

    public static List<Sanpham> TimKiemSanPhamDangThamGiaDauGia(String key, Taikhoan tk, int page) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        List<Sanpham> listSP = null;
        try {
            session.beginTransaction();
            String hql = "select distinct ct.sanpham from Chitietdaugia ct where ct.taikhoan=:tk and ct.sanpham.ngayHetHan >:homnay ";
            hql += "and ct.sanpham.tenSanPham like CONCAT ('%', :tenSP ,'%') ";
            hql += "order by ct.thoiGiandaugia";
            Query query = session.createQuery(hql);
            query.setParameter("tk", tk);
            query.setDate("homnay", new Date());
            query.setString("tenSP", key);

            int numberProducts = 4;
            // Tính số trang :
            listSP = query.list();
            pageCount = tinhTrangTheoDSSP(listSP.size(), numberProducts);
            listCount = listSP.size();

            int n = (page - 1) * numberProducts;

            if (numberProducts != -1) {
                query.setFirstResult(n);
                query.setMaxResults(numberProducts);
            }
            listSP = query.list();

        } catch (Exception ex) {
            listSP = null;
        } finally {
            session.close();
        }
        return listSP;
    }

    public static List<Chitietdaugia> LayChiTietLichSuDauGia(Taikhoan tk, int page) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        List<Chitietdaugia> listCT = null;
        try {
            session.beginTransaction();
            String hql = "select distinct ct from Chitietdaugia ct where ct.taikhoan=:tk ";
            hql += "order by ct.thoiGiandaugia";
            Query query = session.createQuery(hql);
            query.setParameter("tk", tk);
            query.setMaxResults(8);

            int numberProducts = 4;
            // Tính số trang :
            listCT = query.list();
            pageCount = tinhTrangTheoDSSP(listCT.size(), numberProducts);
            listCount = listCT.size();

            int n = (page - 1) * numberProducts;

            if (numberProducts != -1) {
                query.setFirstResult(n);
                query.setMaxResults(numberProducts);
            }
            listCT = query.list();

        } catch (Exception ex) {
            listCT = null;
        } finally {
            session.close();
        }
        return listCT;
    }

    public static Chitietdaugia LayDauGiaChienThang(int masp, long giaCT) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Chitietdaugia dgCT = null;

        try {

            session.beginTransaction();

            String hql = "from Chitietdaugia ct where ct.sanpham.maSanPham =:masp and ct.giaDau =:giaCT";
            Query query = session.createQuery(hql);
            query.setInteger("masp", masp);
            query.setLong("giaCT", giaCT);

            List<Chitietdaugia> listCT = query.list();
            if (listCT != null && listCT.isEmpty() == false) {
                dgCT = listCT.get(0);
            }

        } catch (Exception ex) {
            dgCT = null;
        } finally {
            session.close();
        }

        return dgCT;
    }

    public static List<Chitietdaugia> TimKiemChiTietLichSuDauGia(String key, Taikhoan tk, int page) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        List<Chitietdaugia> listCT = null;
        try {
            session.beginTransaction();
            String hql = "select distinct ct from Chitietdaugia ct where ct.taikhoan=:tk ";
            hql += "and ct.sanpham.tenSanPham like CONCAT ('%', :tenSP ,'%') ";
            hql += "order by ct.thoiGiandaugia";
            Query query = session.createQuery(hql);
            query.setParameter("tk", tk);
            query.setString("tenSP", key);

            int numberProducts = 4;
            // Tính số trang :
            listCT = query.list();
            pageCount = tinhTrangTheoDSSP(listCT.size(), numberProducts);
            listCount = listCT.size();

            int n = (page - 1) * numberProducts;

            if (numberProducts != -1) {
                query.setFirstResult(n);
                query.setMaxResults(numberProducts);
            }
            listCT = query.list();

        } catch (Exception ex) {
            listCT = null;
        } finally {
            session.close();
        }
        return listCT;
    }
    
    public static Chitietdaugia LayChiTietDauGiaGanNhat() {
        
        Session session = HibernateUtil.getSessionFactory().openSession();
        Chitietdaugia ctdg = null;
        try{            
            session.beginTransaction();            
            String hql = "from Chitietdaugia";
            Query query = session.createQuery(hql);
            List<Chitietdaugia> listDG = query.list();
            ctdg = listDG.get(listDG.size() - 1);
        }
        catch(Exception ex){
            ctdg = null;
        }finally{
            session.close();
        }
        
        return ctdg;
    }
}
